import { BaseResult } from '@/types/axios';
import request from '@/Newapi/request/request';

export interface FileGroup {
  id: number;
  created_at: string;
  updated_at: string;
  name: string;
  parent_id: number | null;
  children: FileGroup[] | null;
}

export interface CreateFileGroupParams {
  name: string;
  parent_id: number | null;
}

export interface FileGroupCheckResult {
  children: FileGroup[];
  files: {
    id: number;
    created_at: string;
    updated_at: string;
    file_name: string;
    file_path: string;
    file_url: string;
    type: number;
    uploader: number;
    group_id: number;
  }[];
  group_id: number;
  has_children: boolean;
  has_files: boolean;
}

export interface FileItem {
  id: number;
  created_at: string;
  updated_at: string;
  file_name: string;
  file_path: string;
  file_url: string;
  type: number; // 1图片，2视频，3html，4其他
  uploader: number;
  group_id: number;
}

class FileGroupServiceImpl {
  private baseUrl = '/static';

  /**
   * 获取文件组列表
   */
  async getFileGroupList(): Promise<BaseResult<FileGroup[]>> {
    try {
      const response = await request.get<FileGroup[]>(`${this.baseUrl}/fileGroup/list`);
      return {
        code: response.code,
        message: response.message || response.msg || '获取文件组列表成功',
        data: response.data || []
      };
    } catch (error) {
      console.error('获取文件组列表失败:', error);
      return {
        code: 500,
        message: '获取文件组列表失败',
        data: []
      };
    }
  }

  /**
   * 创建文件组
   */
  async createFileGroup(params: CreateFileGroupParams): Promise<BaseResult> {
    try {
      const response = await request.post(`${this.baseUrl}/create`, params);
      return {
        code: response.code,
        message: response.message || response.msg || '创建文件组成功',
        data: response.data
      };
    } catch (error) {
      console.error('创建文件组失败:', error);
      return {
        code: 500,
        message: '创建文件组失败',
        data: null
      };
    }
  }

  /**
   * 新增文件组
   */
  async addFileGroup(params: { name: string; id: number | null }): Promise<BaseResult> {
    try {
      const response = await request.post(`${this.baseUrl}/fileGroup/add`, {
        name: params.name,
        parent_id: params.id === null ? 0 : params.id
      });
      return {
        code: response.code,
        message: response.message || '新增文件组成功',
        data: response.data
      };
    } catch (error) {
      console.error('新增文件组失败:', error);
      return {
        code: 500,
        message: '新增文件组失败',
        data: null
      };
    }
  }

  /**
   * 检查文件组
   */
  async checkFileGroup(id: number): Promise<BaseResult<FileGroupCheckResult>> {
    try {
      const response = await request.get<FileGroupCheckResult>(`${this.baseUrl}/fileGroup/check/${id}`);
      return {
        code: response.code,
        message: response.message || response.msg || '检查文件组成功',
        data: response.data || {
          children: [],
          files: [],
          group_id: id,
          has_children: false,
          has_files: false
        }
      };
    } catch (error) {
      console.error('检查文件组失败:', error);
      return {
        code: 500,
        message: '检查文件组失败',
        data: {
          children: [],
          files: [],
          group_id: id,
          has_children: false,
          has_files: false
        }
      };
    }
  }

  /**
   * 删除文件组
   */
  async deleteFileGroup(id: number): Promise<BaseResult> {
    try {
      const response = await request.delete(`${this.baseUrl}/fileGroup/${id}`);
      return {
        code: response.code,
        message: response.message || response.msg || '删除文件组成功',
        data: response.data
      };
    } catch (error) {
      console.error('删除文件组失败:', error);
      return {
        code: 500,
        message: '删除文件组失败',
        data: null
      };
    }
  }

  /**
   * 重命名文件组
   */
  async renameFileGroup(id: number, newName: string): Promise<BaseResult> {
    try {
      const response = await request.put(`${this.baseUrl}/fileGroup/rename/${id}`, { name: newName });
      return {
        code: response.code,
        message: response.message || response.msg || '重命名文件组成功',
        data: response.data
      };
    } catch (error) {
      console.error('重命名文件组失败:', error);
      return {
        code: 500,
        message: '重命名文件组失败',
        data: null
      };
    }
  }

  /**
   * 获取文件列表
   */
  async getFileList(groupId: number): Promise<BaseResult<FileItem[]>> {
    try {
      const response = await request.get<FileItem[]>(`${this.baseUrl}/file/${groupId}`);
      return {
        code: response.code,
        message: response.message || response.msg || '获取文件列表成功',
        data: response.data || []
      };
    } catch (error) {
      console.error('获取文件列表失败:', error);
      return {
        code: 500,
        message: '获取文件列表失败',
        data: []
      };
    }
  }

  /**
   * 编辑文件
   */
  async editFile(fileData: FileItem): Promise<BaseResult> {
    try {
      const response = await request.put(`${this.baseUrl}/file/edit`, fileData);
      return {
        code: response.code,
        message: response.message || '编辑文件成功',
        data: response.data
      };
    } catch (error) {
      console.error('编辑文件失败:', error);
      return {
        code: 500,
        message: '编辑文件失败',
        data: null
      };
    }
  }

  /**
   * 删除文件
   */
  async deleteFile(id: number): Promise<BaseResult> {
    try {
      const response = await request.delete(`${this.baseUrl}/file/${id}`);
      return {
        code: response.code,
        message: response.message || '删除文件成功',
        data: response.data
      };
    } catch (error) {
      console.error('删除文件失败:', error);
      return {
        code: 500,
        message: '删除文件失败',
        data: null
      };
    }
  }
}

export const fileGroupService = new FileGroupServiceImpl(); 